package dao

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/xmls"
	"database/sql"
	_ "github.com/go-sql-driver/mysql" //选择需要的数据库驱动导入
	"github.com/zhuxiujia/GoMybatis"
)

var Engine GoMybatis.GoMybatisEngine
var UserMapper domain.UserMapper
var UrlsMapper domain.UrlsMapper
var SystemMapper domain.SystemMapper
var UserDetailsMapper domain.UserDetailsMapper
var SystemInfoMapper domain.SyetemInfoMapper

// 数据库连接

func DatabaseInit() *sql.DB {
	Engine = GoMybatis.GoMybatisEngine{}.New()
	//Mysql链接格式 用户名:密码@(数据库链接地址:端口)/数据库名称,如root:123456@(***.com:3306)/test
	db, err := Engine.Open("mysql", common.MysqlUserName+":"+common.MysqlPassword+"@tcp("+common.MysqlPath+")/apitest?charset=utf8&loc=Asia%2FShanghai&parseTime=true")
	if err != nil {
		panic(err)
	}
	Engine.WriteMapperPtr(&UserMapper, xmls.UserMapper)
	Engine.WriteMapperPtr(&UrlsMapper, xmls.UrlsMapper)
	Engine.WriteMapperPtr(&SystemMapper, xmls.SystemMapper)
	Engine.WriteMapperPtr(&UserDetailsMapper, xmls.UserDetailsMapper)
	Engine.WriteMapperPtr(&SystemInfoMapper, xmls.SystemInfoMapper)
	return db
}
